package com.party.service.study.impl;

import com.party.mapper.User2ContentMapper;
import com.party.mapper.User2TestMapper;
import com.party.model.*;
import com.party.service.study.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class User2TestServiceImpl implements User2TestService
{
    @Resource
    User2TestMapper user2TestMapper;
    @Resource
    User2ContentMapper user2ContentMapper;
    @Resource
    User2curriculumService user2curriculumService;
    @Resource
    TestService testService;
    @Resource
    AnswerService answerService;
    @Override
    public int add(User2Test obj,int preScore) {
        if(obj != null)
        {
            boolean right = this.dealSelect(obj);
            //答对了
            if(right)
            {
                //给user2Curriculum加分
                User2Curriculum user2Curriculum = this.user2curriculumService.get(obj.getUid(),obj.getCurriculumid());
                if(user2Curriculum != null)
                {
                    Integer score = user2Curriculum.getScore();
                    if(score==null || score <0)
                    {
                        score =0;
                    }
                    score = score+preScore;//加上得分
                    user2Curriculum.setScore(score);
                    this.user2curriculumService.update(user2Curriculum);
                }
            }
            return this.user2TestMapper.insertSelective(obj);
        }
        return 0;
    }

    @Override
    public int add(List<User2Test> user2TestList,int preScore) {
        if(user2TestList != null && user2TestList.size() >0)
        {
            int addScore = 0;//加上的分数
            for(User2Test obj:user2TestList)
            {
                boolean right = this.dealSelect(obj);
                if(right)
                {
                    addScore = addScore +preScore;
                }
                this.user2TestMapper.insertSelective(obj);
            }
            if(addScore > 0)
            {
                //给user2Curriculum加分
                User2Curriculum user2Curriculum = this.user2curriculumService.get(user2TestList.get(0).getUid(),user2TestList.get(0).getCurriculumid());
                if(user2Curriculum != null)
                {
                    Integer score = user2Curriculum.getScore();
                    if(score==null || score <0)
                    {
                        score =0;
                    }
                    score = score+addScore;//加上得分
                    user2Curriculum.setScore(score);
                    this.user2curriculumService.update(user2Curriculum);
                }
            }
            return user2TestList.size();
        }
        return 0;
    }

    @Override
    public List<User2Test> getByCurrId(Integer curriculumId) {
        if(curriculumId != null && curriculumId > 0)
        {
            User2TestExample example = new User2TestExample();
            User2TestExample.Criteria criteria = example.createCriteria();

            criteria.andCurriculumidEqualTo(curriculumId);
            return this.user2TestMapper.selectByExample(example);
        }
        return null;
    }

    /**
     * 看这个题选择是否正确
     * @return true正确，false错误
     */
    private boolean dealSelect(User2Test obj)
    {
        boolean isRight = false;
        if(obj !=null) {
//            Test test = this.testService.getById(obj.getTestid());
            //判断这个题目选对了没有
            List<Answer> answerList = this.answerService.getByTestId(obj.getTestid());
            isRight = true;
            if (answerList != null) {
                String[] selects = obj.getAnswerList();
                for (Answer answer : answerList) {
                    String id = answer.getId() + "";
                    String right = answer.getIsright();//1是正确选项，0不是
                    boolean in = false;
                    for (String select : selects) {
                        //确保每个正确答案都选了
                        if (right.equals("1") && select.equals(id)) {
                            in = true;
                        }
                        //这个选项用户选了的
                        if (select.equals(id) && right.equals("0")) {
                            isRight = false;
                            break;
                        }
                    }
                    //这个选项是对的，但是又没有选，直接判错
                    if (right.equals("1") && !in) {
                        isRight = false;
                        break;
                    }
                }
            }
            //这个题做对了
            if (isRight) {
                obj.setIsright(1);
            } else {
                obj.setIsright(0);
            }
        }
        return isRight;
    }
}
